﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using JXC.Core.Models;
using JXC.EFCore;
using AutoMapper;
using JXC.Application.Dtos;
using JXC.EFCore.Helper;

namespace JXC.WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CaigouReturnedsController : BaseController
    {
        private readonly AppDbContext _context;
        private readonly IMapper _mapper;

        public CaigouReturnedsController(
            AppDbContext context,
            IMapper mapper
            )
        {
            _context = context;
            _mapper = mapper;
        }

        // GET: api/CaigouReturneds
        [HttpGet]
        public async Task<ActionResult<IEnumerable<CaigouReturned>>> GetCaigouReturned()
        {
            return await _context.CaigouReturned.Include(p => p.ReturnedDetails).ToListAsync();
        }

        // GET: api/CaigouReturneds/5
        [HttpGet("{id}")]
        public async Task<ActionResult<CaigouReturned>> GetCaigouReturned(Guid id)
        {
            var caigouReturned = await _context.CaigouReturned.FindAsync(id);

            if (caigouReturned == null)
            {
                return NotFound();
            }

            return caigouReturned;
        }

        // PUT: api/CaigouReturneds/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutCaigouReturned(Guid id, CaigouReturned caigouReturned)
        {
            if (id != caigouReturned.Id)
            {
                return BadRequest();
            }

            _context.Entry(caigouReturned).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CaigouReturnedExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/CaigouReturneds
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<CaigouReturned>> PostCaigouReturned(CaigouReturnedDto caigouReturnedDto)
        {
            try
            {
                CaigouReturned caigouReturned = _mapper.Map<CaigouReturned>(caigouReturnedDto);
                caigouReturned.ExamineState = 2;
                _context.CaigouReturned.Add(caigouReturned);
                foreach (var item in caigouReturnedDto.ReturnedDetailDtos)
                {
                    item.CaigouReturnedId = caigouReturned.Id;
                }

                List<ReturnedDetail> returnedDetails = _mapper.Map<List<ReturnedDetail>>(caigouReturnedDto.ReturnedDetailDtos);
                foreach (var item in returnedDetails)
                {
                    item.Id = Guid.NewGuid();
                }
                _context.ReturnedDetail.AddRange(returnedDetails);
                await _context.SaveChangesAsync();

                return CreatedAtAction("GetCaigouReturned", new { id = caigouReturned.Id }, DataResult(caigouReturned));
            }
            catch (Exception e)
            {

                throw;
            }

        }

        // DELETE: api/CaigouReturneds/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteCaigouReturned(Guid id)
        {
            var caigouReturned = await _context.CaigouReturned.FindAsync(id);
            if (caigouReturned == null)
            {
                return NotFound();
            }

            _context.CaigouReturned.Remove(caigouReturned);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool CaigouReturnedExists(Guid id)
        {
            return _context.CaigouReturned.Any(e => e.Id == id);
        }
    }
}
